package com.example.jbmsuppliers.sinopec.doorjurisd;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.IEGuardKey;
import com.jbm.jbmsupplier.util.IEGuardKeyCtrl;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class DoorManageActivity extends ActivityUse implements AdapterView.OnItemClickListener {


    @InjectView(R.id.goback_ps_set)
    Button gobackPsSet;
    @InjectView(R.id.titlelz)
    TextView titlelz;
    @InjectView(R.id.id_door_address_list)
    ListView idDoorAddressList;
    private ArrayList<IEGuardKey> mDataList = new ArrayList<IEGuardKey>();
    private DialogLoading dialog;
    private DooarAddressAdapter mDooarAdapter;
    public static final int AUTHORADDRESSMESS = 0001;
    private final int EMPIWE = 0002;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case AUTHORADDRESSMESS: {
                    mDataList.clear();
                    IEGuardKeyCtrl restlu = (IEGuardKeyCtrl) msg.obj;
                    if (restlu.back) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        for (int i = 0; i < restlu.count; i++) {
                            IEGuardKey resultlist = restlu.IndexofList(i);
                            if (resultlist.open_total == -1) {
                                mDataList.add(resultlist);
                            }
                        }
                        mDooarAdapter = new DooarAddressAdapter(DoorManageActivity.this, mDataList);
                        idDoorAddressList.setAdapter(mDooarAdapter);
                        mDooarAdapter.notifyDataSetChanged();

                    } else if (restlu.equals("time out")) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        hint(restlu.errorinfo);

                    } else {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        hint(restlu.errorinfo);
                    }

                    break;
                }
                case EMPIWE: {
                    int position = (int) msg.obj;
                    setEmpiwe(position);
                    break;
                }

            }
        }
    };

    @Override
    protected int setContentView1() {
        return R.layout.activity_door_manage;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        ExitAppUtlis.getInstance().addActivity(this);

        idDoorAddressList.setOnItemClickListener(this);
        if (getIntent().getStringExtra("shouquan").equals("shouquan")) {
            titlelz.setText("开门授权");
        }
        NetWork networt = new NetWork(handler);
        networt.getAuthorAddress();
        dialog = new DialogLoading(this, "加载中...", true);
        dialog.show();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        handler.sendMessage(handler.obtainMessage(EMPIWE, position));
    }

    private void setEmpiwe(int position) {//跳转到开门授权页面
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), EmpowerActivity.class);
        intent.putExtra("id", mDataList.get(position).mac);
        intent.putExtra("egkey_id", mDataList.get(position).egkey_id);
        startActivity(intent);
        finish();
    }

    @OnClick(R.id.goback_ps_set)
    public void onClick() {
        finish();
    }

    class DooarAddressAdapter extends BaseAdapter {
        private Context context = null;
        public ArrayList<IEGuardKey> mAddressList = null;

        public DooarAddressAdapter(Context context, ArrayList<IEGuardKey> inList) {
            this.context = context;
            this.mAddressList = inList;
        }

        @Override
        public int getCount() {
            return mAddressList.size();
        }

        @Override
        public Object getItem(int position) {
            return mAddressList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            try {
                if (convertView == null) {
                    convertView = LayoutInflater.from(this.context).inflate(R.layout.door_item, null);
                }
                TextView addressText = (TextView) convertView.findViewById(R.id.door_item_address);
                final IEGuardKey info = mAddressList.get(position);

                if (info == null)
                    return null;

                if (info.gi_fulltext != null)
                    addressText.setText(info.gi_fulltext);
                else
                    addressText.setText(info.open_key);
                convertView.setTag(info);

                return convertView;
            } catch (Exception e) {
                String msg = e.getMessage();
                return null;
            }
        }
    }


}
